#include "StdAfx.h"
#include "IPCV2Server.h"
#include "IPCV2ServerNotify.h"

CIPCV2Server::CIPCV2Server(wstring localIP, int localPort, ISysLog* pLog):
	m_strLocalIP(localIP),
	m_nLocalPort(localPort),
	m_pSysLog(pLog)
{
	m_pServerNotify = new CIPCV2ServerNotify;
	m_pIPCV2Server	= NULL;
	m_bStartServer	= false;
}

CIPCV2Server::~CIPCV2Server(void)
{
	if (m_pServerNotify)
	{
		delete m_pServerNotify;
		m_pServerNotify = NULL;
	}
	m_bStartServer = false;
}

bool CIPCV2Server::StartServer()
{
	if (!m_pIPCV2Server)
	{
		ProcessCommV2::ServerInitParam serverInit;
		serverInit.type		= ProcessCommV2::PC_TYPE_TCP;
		serverInit.pLog		= m_pSysLog;
		serverInit.pNotify	= m_pServerNotify;
		wcscpy_s(serverInit.szAddr, m_strLocalIP.c_str());
		serverInit.nAddrPort= m_nLocalPort;
		m_pIPCV2Server = CSingleton<CProcessCommDLLMag>::Instance()->CreateProcessServer(serverInit);
	}
	m_bStartServer = true;
	return m_pIPCV2Server->StartServer();
}

bool CIPCV2Server::StopServer()
{
	if (!m_pIPCV2Server) return false;
	if (!m_bStartServer) return false;
	m_bStartServer = false;
	bool bResult = m_pIPCV2Server->StopServer();

	CSingleton<CProcessCommDLLMag>::Instance()->DestroyProcessServer(m_pIPCV2Server);
	m_pIPCV2Server = NULL;

	return bResult;
}